/**
 *                     Remixvym (version 0.8.4)
 *             Copyright (c) 2010 by RemixLab, DISI-UNAL
 *            http://www.disi.unal.edu.co/grupos/remixlab/
 *
 * This Java package provides classes to visualize Mind Maps created in VYM
 * (View Your Mind) in a Processing Sketch
 *
 * @author Alvaro Lopez, Juan Baquero
 *
 * This source file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * A copy of the GNU General Public License is available on the World Wide Web
 * at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by
 * writing to the Free Software Foundation, 51 Franklin Street, Fifth Floor
 * Boston, MA 02110, USA
 */
package remixlab.remixvym.core.util.selected;

import java.util.ArrayList;

import processing.core.PFont;
import remixlab.remixvym.MindMap;
import remixlab.remixvym.core.graphics.StyleGraph;
import remixlab.remixvym.core.graphics.node.NodeConstans;
import remixlab.remixvym.core.graphics.text.TextUtil;
import remixlab.remixvym.core.mindmap.Node;


public class QuadSelectedUtil implements SelectedUtil,NodeConstans{
	

	public void up(Node node, MindMap map, StyleGraph style) {
		
		int i;
		if(!node.selected)
		{
			//CALCULA EL ANCHO DEL NODO, PRIMERO POR EL ANCHO DEL TEXTO
			//LUEGO POR EL NUMERO DE EMOTICONES CON SUS ESPACIOS ENTRE SI
			//Y POR ULTIMO EL ESPACIO PARA ICONOS
			
			PFont font;
			float heigth;
			float defHeigth;
			float sizeDown;
			
			if(node.getFather()==null){
				sizeDown = style.sizeFontRoot;
				font = style.fontRootSelected;
				heigth = style.heigthRoot;
				defHeigth = style.defHeigthRoot;
			}
			else if(node.getFather().getFather()==null){
				sizeDown = style.sizeFontChild;
				font = style.fontChildSelected;
				heigth = style.heigthChild;
				defHeigth = style.defHeigthChild;
			}
			else{ 
				sizeDown = style.sizeFontNormal;
				font = style.fontSelected;
				heigth = style.heigthNormal;
				defHeigth = style.defHeigthNormal;
			}
			
			int heigthNode = (int)((heigth*font.getFont().getSize())/sizeDown);
			
			int initSpace = (int)((heigthNode*defInitSpace)/defHeigth);
			int spaceObjects = (int)((heigthNode*defSpaceObjects)/defHeigth);
			int longEmoticon = (int)((heigthNode*defLongEmoticon)/defHeigth);
			int spaceIcons = (int)((heigthNode*defSpaceIcons)/defHeigth);
			
			if(longEmoticon>defLongEmoticon) longEmoticon=defLongEmoticon;
			if(spaceObjects>defSpaceObjects) spaceObjects=defSpaceObjects;
			if(initSpace>defInitSpace) initSpace=defInitSpace;
			
			//LISTA QUE GUARDA EL TEXTO PARA LOS NODOS MULTI-LINEA
			
			ArrayList<String> list = TextUtil.getSize(style, font, node.getInfo());
			
			int wdText = Integer.parseInt(list.get(list.size()-1));
			
			int widthNode = initSpace+wdText;
			
			for(i=0;node.emoticones!=null && i<node.emoticones.size();i++)
				widthNode = widthNode+longEmoticon+spaceObjects;

			widthNode = widthNode+initSpace+spaceIcons;
			
			heigthNode = (heigthNode*(list.size()-1));

			
			incrX = (int)((widthNode-node.width)/2);
			incrY = (int)((heigthNode-node.height)/2);
			
			if(widthNode<node.width)
				incrX=-incrX;
			
//			if(Math.abs(incrX)<(heigth*def10Space)/defHeigth)
//				incrX=(heigth*def10Space)/defHeigth;

			selected = node;
			
			searchUp(map.getMap());

			node.x1 = node.x1 - incrX;
			node.y1 = node.y1 - incrY;
			node.y2 = node.y2 + incrY;
			node.x2 = node.x2 + incrX;
			node.calculate();
			node.selected=true;
		}

	}

	public void down(Node node,MindMap map)
	{
		if(incrX==0 && incrY==0)return;
		
		if(node!=null && node.selected)
		{
			selected = node;
			
			searchDown(map.getMap());
			
			node.x1 = node.x1 + incrX;
			node.y1 = node.y1 + incrY;
			node.y2 = node.y2 - incrY;
			node.x2 = node.x2 - incrX;
			node.calculate();

			node.selected = false;
			
			incrX=0;
			incrY=0;
		}
	
	}
	
	//-------------   Movimiento de Nodos ------------------
	
	private void searchUp(Node node) {
		
		if(node!=selected)
		{
			int cox = (int)(node.x1 + (node.width/2));   //Centro en X del nodo a mover
			int coy = (int)(node.y1 + (node.height/2));  //Centro en Y del nodo a mover
			
			int incX =0; //Empuje al nodo a mover en X;
			int incY =0; //Enpuje al nodo a mover en Y;
			
			//MOVIMIENTO EN X
			
			if(cox<selected.x1) incX = -incrX;
			if(cox>selected.x2) incX = incrX;
			
			node.x1 = node.x1+incX;
			node.x2 = node.x2+incX;
			
			cox = (int)(node.x1 + (node.width/2));   //Centro en X del nodo a mover
			coy = (int)(node.y1 + (node.height/2));  //Centro en Y del nodo a mover
			
			if(coy<selected.y1) incY = -incrY;
			if(coy>selected.y2) incY = incrY;
			
			//MOVIMIENTO EN Y
			
			node.y1 = node.y1+incY;
			node.y2 = node.y2+incY;

		}

		for(int i=0 ; i<node.children.size();i++)
		{
			if(!node.children.get(i).hide)
				searchUp(node.children.get(i));
		}
	}
	
	private void searchDown(Node node) {
		
		if(node!=selected)
		{
			int cox = (int)(node.x1 + (node.width/2));   //Centro en X del nodo a mover
			int coy = (int)(node.y1 + (node.height/2));  //Centro en Y del nodo a mover
			
			int incX =0; //Empuje al nodo a mover en X;
			int incY =0; //Enpuje al nodo a mover en Y;
			 
			//MOVIMIENTO EN Y
			
			if(coy<selected.y1) incY = -incrY;
			if(coy>selected.y2) incY = incrY;

			node.y1 = node.y1-incY;
			node.y2 = node.y2-incY;
			
			cox = (int)(node.x1 + (node.width/2));   //Centro en X del nodo a mover
			coy = (int)(node.y1 + (node.height/2));  //Centro en Y del nodo a mover
			
			if(cox<selected.x1) incX = -incrX;
			if(cox>selected.x2) incX = incrX;
			
			//MOVIMIENTO EN X

			node.x1 = node.x1-incX;
			node.x2 = node.x2-incX;
		}

		for(int i=0 ; i<node.children.size();i++)
		{
			if(!node.children.get(i).hide)
				searchDown(node.children.get(i));
		}
	}
	
	private Node selected;
	
	private int incrY;
	private int incrX;
	

}
